package com.gk.util.component.pageupdown
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 *翻页工具类
	 * 
	 * <br> 指定上下按钮，翻页范围  1-最大值  
	 * @author Nestor
	 * 
	 */	
	public class PageUpDown
	{
		private var changeCallBk:Function;
		private var _crtPage:int;
		private var _maxPage:int;
		private var allUpBtn:InteractiveObject;
		private var allDownBtn:InteractiveObject;
		private var upBtn:InteractiveObject;
		private var downBtn:InteractiveObject;
		/**
		 * 
		 * @param upBtn 上按钮
		 * @param downBtn 下按钮
		 * @param change 翻页回调（参数-当前页）  当前页范围 1-最大值
		 * @param crtPage 当前页
		 * @param maxPage 最大页
		 * 
		 */		
		public function PageUpDown(upBtn:InteractiveObject,downBtn:InteractiveObject,change:Function,crtPage:int=1,maxPage:int=10)
		{
			this.changeCallBk = change;
			_crtPage = crtPage;
			_maxPage = maxPage;
			this.upBtn = upBtn;
			this.downBtn = downBtn;
			this.upBtn.addEventListener(MouseEvent.CLICK,upHandler);
			this.downBtn.addEventListener(MouseEvent.CLICK,downHandler);
			this.upBtn.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			this.downBtn.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
		}
		
		//		++++++++++++++++++  public  mothed    ++++++++++++++++++++++
		/**
		 *设置首尾页按钮 
		 * 
		 * @param allUpBtn 首页
		 * @param allDownBtn 尾页
		 * 
		 */
		public function setAllUpDwonBtn(allUpBtn:InteractiveObject,allDownBtn:InteractiveObject):void{
			
			
			if(this.allUpBtn != null){
				this.allUpBtn.removeEventListener(MouseEvent.CLICK,allUpHandler);
				this.allUpBtn.removeEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
				this.allUpBtn = null;
			}
			if(this.allDownBtn != null){
				this.allDownBtn.removeEventListener(MouseEvent.CLICK,allDownHandler);
				this.allDownBtn.removeEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
				this.allDownBtn = null;
			}
			
			this.allUpBtn = allUpBtn;
			this.allDownBtn = allDownBtn;
			this.allUpBtn.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			this.allUpBtn.addEventListener(MouseEvent.CLICK,allUpHandler);
			this.allDownBtn.addEventListener(MouseEvent.CLICK,allDownHandler);
			this.allDownBtn.addEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
			
		}
		
		
		//		++++++++++++++++++  event Handler   ++++++++++++++++++++++++++++++++++
		
		protected function allDownHandler(event:MouseEvent):void
		{
			if(this.crtPage != maxPage){
				this.crtPage = maxPage;
				this.changeCallBk(this.crtPage);
			}
		}
		
		protected function allUpHandler(event:MouseEvent):void
		{
			if(this.crtPage != 1){
				this.crtPage = 1;
				this.changeCallBk(this.crtPage);
			}
		}	
		
		protected function downHandler(event:MouseEvent):void
		{
			this.crtPage++;
			if(this.crtPage > maxPage){
				this.crtPage = 	maxPage;			
			}else{
				this.changeCallBk(this.crtPage);
			}
		}
		
		protected function upHandler(event:MouseEvent):void
		{
			this.crtPage--;
			if(this.crtPage < 1){
				this.crtPage = 	1;			
			}else{
				this.changeCallBk(this.crtPage);
			}
		}
		
		private function removeFromStage(e:Event):void{
			switch(e.currentTarget){	
				case this.allUpBtn:
					this.allUpBtn.removeEventListener(MouseEvent.CLICK,allUpHandler);
					break;
				case this.allDownBtn:
					this.allDownBtn.removeEventListener(MouseEvent.CLICK,allDownHandler);
					break;
				case this.upBtn:
					this.upBtn.removeEventListener(MouseEvent.CLICK,upHandler);
					break;
				case this.downBtn:
					this.downBtn.removeEventListener(MouseEvent.CLICK,downHandler);
					break;
			}
			InteractiveObject(e.currentTarget).removeEventListener(Event.REMOVED_FROM_STAGE,removeFromStage);
		}
		
		//		++++++++++++++++++   get  set    ++++++++++++++++++++++
		
		public function get crtPage():int
		{
			return _crtPage;
		}
		
		public function set crtPage(value:int):void
		{
			_crtPage = value;
		}
		
		public function get maxPage():int
		{
			return _maxPage;
		}
		
		public function set maxPage(value:int):void
		{
			_maxPage = value;
			crtPage = 1;
		}
		
		
		
	}
}